// 域名
export const baseUrl = "http://aph.Junhecms.com"

export const pageJump = (item) => {
	if (parseInt(item.jumpdata) === 0) return;
	else if (parseInt(item.jumpdata) === 1) {
		// 产品
		uni.navigateTo({
			url: `/homePage/ecology/info?navTitle=数据分析管理&id=${item.jumpid}`
		});
	} else if (parseInt(item.jumpdata) === 2) {
		// 新闻
		if (!item.url) return;
		uni.navigateTo({
			url: '/homePage/outside/index?url=' + item.url
		});
	} else if (parseInt(item.jumpdata) === 3) {
		// 白皮书
		uni.navigateTo({
			url: '/homePage/professionInfo/index?titleType=4' + `&id=${item.jumpid}`
		});
	} else if (parseInt(item.jumpdata) === 4) {
		// 产品列表
		uni.navigateTo({
			url: '/pages/product/index'
		});
	} else if (parseInt(item.jumpdata) === 5) {
		// 白皮书列表
		uni.navigateTo({
			url: '/pages/profession/index'
		});
	}
}

/**
 * 获取顶部距离及胶囊信息
 * @returns {{mgTop: *, linHeight}}
 */
export const getUpperHeight = () => {
	const systemInfo = uni.getSystemInfoSync();
	const statusBarHeight = systemInfo.statusBarHeight;
	// #ifdef MP-WEIXIN
	let menuButtonRect = uni.getMenuButtonBoundingClientRect();
	// #endif

	return {
		mgTop: statusBarHeight,
		linHeight: menuButtonRect?.height
	};
};

// 提示显示时间及部分跳转时间
const duration = 1500;

// 提示
export const msg = (title) => {
	uni.showToast({
		title,
		mask: true,
		icon: 'none',
		duration: duration
	});
};
export const msgSuccess = (title) => {
	uni.showToast({
		title,
		mask: true,
		icon: 'success'
	});
};
export const msgError = (title) => {
	uni.showToast({
		title,
		mask: true,
		icon: 'error'
	});
};

// 跳转
const jump = (url, code) => {
	setTimeout(() => {
		if (code === 1) {
			uni.navigateTo({
				url
			});
		} else if (code === 2) {
			uni.redirectTo({
				url
			});
		} else if (code === 3) {
			uni.reLaunch({
				url
			});
		} else if (code === 4) {
			uni.navigateBack();
		} else if (code === 5) {
			uni.switchTab({
				url
			});
		}
	}, duration);
};
export const navigateTo = (url) => jump(url, 1);
export const redirectTo = (url) => jump(url, 2);
export const reLaunch = (url) => jump(url, 3);
export const navigateBack = () => jump(url, 4);
export const switchTab = () => jump(url, 5);

// 防抖
let shakingTimer = null;
export const shaking = (fun, delay = 600) => {
	return () => {
		if (shakingTimer) {
			clearTimeout(shakingTimer);
			shakingTimer = null;
		}
		shakingTimer = setTimeout(() => {
			fun();
		}, delay);
	};
};
// 节流
let reduceTimer = null;
export const reduce = (fun, delay = 500) => {
	return () => {
		if (reduceTimer) return;
		reduceTimer = setTimeout(() => {
			fun();
			clearTimeout(reduceTimer);
			reduceTimer = null;
		}, delay);
	};
};